﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace ExcelAutomator.Validation
{
	public class DirectoryValidator : BaseValidator
	{
		private readonly string m_DirectoryPath;

		public DirectoryValidator(string directoryPath)
		{
			m_DirectoryPath = directoryPath;
		}

		public DirectoryValidator DirectoryExists()
		{
			if (File.Exists(m_DirectoryPath))
			{
				return this;
			}

			if (!Directory.Exists(m_DirectoryPath))
			{
				SetFailure(string.Format("The directory '{0}' doesn't exists.", m_DirectoryPath), "DirectoryExists");
			}
			return this;
		}

		public DirectoryValidator ContainsFilesWithExtensions(IEnumerable<string> fileExtensions)
		{
			#region pre
			if (fileExtensions == null)
			{
				throw new ArgumentNullException("fileExtensions");  //NON-NLS-1
			}
			if (!Directory.Exists(m_DirectoryPath))
			{
				return this;
			}

			#endregion

			var files = new List<string>();
			foreach (var fileExtension in fileExtensions)
			{
				
				files.AddRange(Directory.GetFiles(m_DirectoryPath, fileExtension));
			}

			if (files.Count == 0)
			{
				SetFailure(
					string.Format("The specified folder '{0}' doesn't contain any files with '{1}' extension.", 
					m_DirectoryPath, 
					fileExtensions.Aggregate((i,j)=> i + "," + j)),
					"ContainsFilesWithExtensions");
			}
			return this;
		}
	}
}
